import worker, { ThreadWorkerGlobalScope, MessageEvents, ErrorEvent } from '@ohos.worker';

const workerPort: ThreadWorkerGlobalScope = worker.workerPort;

/**
 * 处理来自主线程的消息
 * @param e message data
 */
workerPort.onmessage = function (e: MessageEvents) {

  console.info("onmessage: " + e)
  // 发送消息到主线程
  workerPort.postMessage("message from worker thread.")

}

/**
 * Defines the event handler to be called when the worker receives a message that cannot be deserialized.
 * The event handler is executed in the worker thread.
 *
 * @param e message data
 */
workerPort.onmessageerror = function (e: MessageEvents) {
}

/**
 * Defines the event handler to be called when an exception occurs during worker execution.
 * The event handler is executed in the worker thread.
 *
 * @param e error message
 */
workerPort.onerror = function (e: ErrorEvent) {
}